<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css" />
    <link rel="stylesheet" type="text/css" href="https://a.amap.com/jsapi_demos/static/demo-center/css/prety-json.css">
    <link rel="stylesheet" href="https://cache.amap.com/lbs/static/main1119.css"/>
    <script type="text/javascript">
        window._AMapSecurityConfig = {
            securityJsCode:'b9ba28769560d74a4851d69b3f1f0ce2',
        }
    </script>
    <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=a72a8ca78008524d162e8fe2cc2d84b1"></script>
    <script type="text/javascript" src="https://cache.amap.com/lbs/static/addToolbar.js"></script>
    <style>
        html,
        body,
        #container {
          width: 100%;
          height: 100%;
        }
        #panel {
            position: absolute;
            background-color: white;
            max-height: 90%;
            overflow-y: auto;
            top: 10px;
            right: 10px;
            width: 280px;
        }
      </style>
      <title>获取搜索信息</title>
</head>
<body>
<div id="container"></div>
<div id="panel"></div>
<!--<div class="info">
  <h4>搜索结果展示</h4>
  <p><span id="input-info"></span></p>
</div>-->

<script src="https://webapi.amap.com/maps?v=1.4.15&key=a58ed7280145bdd91cc9a18780d92fd9&plugin=AMap.PlaceSearch"></script>
<script type="text/javascript" src="https://a.amap.com/jsapi_demos/static/demo-center/js/jquery-1.11.1.min.js" ></script>
<script type="text/javascript" src="https://a.amap.com/jsapi_demos/static/demo-center/js/underscore-min.js" ></script>
<script type="text/javascript" src="https://a.amap.com/jsapi_demos/static/demo-center/js/backbone-min.js" ></script>
<script type="text/javascript" src='https://a.amap.com/jsapi_demos/static/demo-center/js/prety-json.js'></script>
<script src="qwebchannel.js"></script>
<script type="text/javascript">
    var map = new AMap.Map("container", {
        resizeEnable: true,
                zoom:11, //初始化地图层级
        center: [116.397428, 39.90923] //初始化地图中心点
    });
    AMap.plugin(["AMap.Autocomplete"], function() {
        //构造地点查询类

    });

        var autoOptions = {
    //city 限定城市，默认全国
    city: '北京'
    }
    var autoComplete= new AMap.Autocomplete(autoOptions);
    //关键字查询
    //placeSearch.search('北京大学');

        var mchannel;
        window.onload=function(){

                if (typeof qt != 'undefined')
                {
                        new QWebChannel(qt.webChannelTransport, function(channel)
                        {
                                channel.objects.qtChannel.cityChanged.connect(autoInput);
                                mchannel=channel;
                                getCurCity();
                        }
                        );
                }
                else
                {
                        alert("qt对象获取失败！");
                }

        }



        function getCurCity()//用来获取当前地图中心点所在城市
        {
                map.getCity(function(place){
                        if(place.province!="")
                        {
                                autoComplete.setCity(place.province);
                                mchannel.objects.qtChannel.cityChangeResult(place.province);
                        }else{
                                autoComplete.setCity(place.city);
                                mchannel.objects.qtChannel.cityChangeResult(place.city);
                        }
                        });
        }
        function testSetCity(city)//用来设置地图显示以及设置"输入提示"功能的城市限制
        {
                console.log(city);
                map.setCity(city,function(){
                        getCurCity();

                });


        }
        function dealComplete(cont)//用来将自动补全API返回的结果传给qt做进一步处理
        {
                console.log(cont);
                autoComplete.search(cont, function(status, result) {
          // 搜索成功时，result即是对应的匹配数据
          //mchannel.objects.qtChannel.getAutocomplete(JSON.stringify(result));
                  console.log(result);
                  mchannel.objects.qtChannel.getAutocomplete(result);
        })
        }






    autoInput();

    AMap.service(["AMap.PlaceSearch"], function() {
        //构造地点查询类
        var placeSearch = new AMap.PlaceSearch({
            pageSize: 5, // 单页显示结果条数
            pageIndex: 1, // 页码
//            city: "010", // 兴趣点城市
            citylimit: false,  //是否强制限制在设置的城市内搜索
            map: map, // 展现结果的地图实例
            panel: "panel", // 结果列表将在此容器中进行展示。
            autoFitView: true // 是否自动调整地图视野使绘制的 Marker点都处于视口的可见范围
        });
        //关键字查询
<!--        placeSearch.search('北京大学');-->
    });


    // 获取搜索信息
    function autoInput(keywords){
    console.log(keywords);
    map.setCity(keywords,function(){
            getCurCity();

    });
      AMap.plugin('AMap.PlaceSearch', function(){
      //构造地点查询类
      var placeSearch = new AMap.PlaceSearch({
          pageSize: 5, // 单页显示结果条数
          pageIndex: 1, // 页码
//        city: "010", // 兴趣点城市
          citylimit: false,  //是否强制限制在设置的城市内搜索
          map: map, // 展现结果的地图实例
          panel: "panel", // 结果列表将在此容器中进行展示。
          autoFitView: true // 是否自动调整地图视野使绘制的 Marker点都处于视口的可见范围
      });
      //关键字查询
      placeSearch.search(keywords);

        var autoOptions = {
          city: '全国'
        }
        var placeSearch = new AMap.PlaceSearch(autoOptions);
        placeSearch.search(keywords, function(status, result) {
          // 搜索成功时，result即是对应的匹配数据
          var node = new PrettyJSON.view.Node({
<!--              el: document.querySelector("#input-info"),-->
              data: result
          });
        })
      })
    }
</script>
</body>
</html>

